** Assemble information on household structure, by month.  Create variables indicating whether
** household includes:
**		- any children under age 19 (or kids who are full-time students and under age 24)
**		- any parents/ any mothers of those kids
** 		- any parents of those parents (and hence grandparents of the children)




**************************************************************************************************

** NOTE: School enrollment status relies on variables
** ENROLD in 1993 panel and earlier (srefmon specific) and RENROLL in later panels



**************************************************************************************************
** 1990
**************************************************************************************************

************
** Wave 3
************

quietly infile using sip90w3.dct, using(s90w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1990_w3, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1990_w3, replace
clear




********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1990_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1990_w3, replace
clear






************
** Wave 4
************

quietly infile using sip90w4.dct, using(s90w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)



** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1990_w4, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1990_w4, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1990_w4, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1990_w4, replace
clear







**************************************************************************************************
** 1991
**************************************************************************************************

************
** Wave 3
************

quietly infile using sip91w3.dct, using(sipp91w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)



** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1991_w3, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1991_w3, replace
clear


********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1991_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1991_w3, replace
clear





**************************************************************************************************
** 1991
**************************************************************************************************

************
** Wave 7
************

quietly infile using sip91w7.dct, using(sipp91w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)



** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1991_w7, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1991_w7, replace
clear

********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1991_w7, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1991_w7, replace
clear






**************************************************************************************************
** 1992
**************************************************************************************************

************
** Wave 4
************

quietly infile using sip92w4.dct, using(sipp92w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)



** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1992_w4, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1992_w4, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1992_w4, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1992_w4, replace
clear






************
** Wave 6
************

quietly infile using sip92w6.dct, using(sipp92w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage


** Identify kids who are under age 19 (hence age-eligible for EITC), not 
** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)



** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)



** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)
** Check that this doesn't write in any 999 values for identifer of parent
assert epn_parent~=999


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)




** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1



** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of potential dependent
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)




** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)


** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)



** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1992_w6, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1992_w6, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child deps living with them,
** but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1992_w6, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1992_w6, replace
clear




**************************************************************************************************
** 1993
**************************************************************************************************

************
** Wave 3
************

quietly infile using sip93w3.dct, using(sipp93w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage



** Identify kids who are under age 19 (hence age-eligible for EITC), not ** married,and living with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)

** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)


** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)

** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if (PotentialDep==1)


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)



** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1

** Drop intermediate variables that were used in the identification of spouses of parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of under-19 kid
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)



** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent = (enrold==1)

** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1993_w3, replace
clear



*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1



keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save OldGenParentData1993_w3, replace
clear


********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1993_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1993_w3, replace
clear




************
** Wave 6
************

quietly infile using sip93w6.dct, using(sipp93w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)

** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)


** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)

** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if PotentialDep==1


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)



** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of under-19 kid
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)



** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent=(enrold==1)

** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace

*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1993_w6, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec

rename socsec essself

duplicates drop

save OldGenParentData1993_w6, replace
clear





************
** Wave 7
************

quietly infile using sip93w7.dct, using(sipp93w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with a parent (mother or father)
gen Under19 = (tage<19 & (ms~=1 & ms~=2))
gen HaveParentinHH = (pnpt~=999)

gen Under19_LiveWithParent = (Under19==1 & HaveParentinHH==1)

** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen Under24FTS_LiveWithParent = (Under24FTS==1 & HaveParentinHH==1)

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)


** Count up number of such kids per household per month. 
sort suid addid srefmon
by suid addid srefmon: egen N_PotentialDep = total(PotentialDep)

** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort suid addid srefmon
by suid addid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed parent of that under-19 kid. 
gen epn_parent=pnpt if PotentialDep==1


** Count up number of distinct parents-of-under-19 kids
egen distinctparenttag = tag(suid addid srefmon epn_parent)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctParents = total(distinctparenttag)



** At household-month level, record person numbers of up to 4 people
** identified as parents of an under-19 kid living with parent
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par1 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par2 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par2
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par3 = min(epn_parent)

replace epn_parent=. if epn_parent==temp_epn_par3
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_par4 = min(epn_parent)

** Generate an indicator, at the individual level, for being a parent of under-19 kid
gen PotentialMidGenParent=(pnum==temp_epn_par1 | pnum==temp_epn_par2 | pnum==temp_epn_par3 | pnum==temp_epn_par4)



** Spouses of potential mid-gen parents could also be considered potential mid-gen parents
gen temp_epn_spouse_par1 = pnsp if pnum==temp_epn_par1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par1 = min(temp_epn_spouse_par1)
gen SpouseOfParent1=(pnum==epn_spouse_par1)
replace PotentialMidGenParent=1 if SpouseOfParent1==1

gen temp_epn_spouse_par2 = pnsp if pnum==temp_epn_par2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par2 = min(temp_epn_spouse_par2)
gen SpouseOfParent2=(pnum==epn_spouse_par2)
replace PotentialMidGenParent=1 if SpouseOfParent2==1

gen temp_epn_spouse_par3 = pnsp if pnum==temp_epn_par3
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par3 = min(temp_epn_spouse_par3)
gen SpouseOfParent3=(pnum==epn_spouse_par3)
replace PotentialMidGenParent=1 if SpouseOfParent3==1

gen temp_epn_spouse_par4 = pnsp if pnum==temp_epn_par4
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_par4 = min(temp_epn_spouse_par4)
gen SpouseOfParent4=(pnum==epn_spouse_par4)
replace PotentialMidGenParent=1 if SpouseOfParent4==1

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_par1 temp_epn_par2 temp_epn_par3 temp_epn_par4 temp_epn_spouse_par1 temp_epn_spouse_par2 temp_epn_spouse_par3 temp_epn_spouse_par4

** Generate indicator for being a female parent of under-19 kid
gen PotentialMidGenMom = (PotentialMidGenParent==1 & sex==2)



** Now identify if the PotentialMidGenParent has one of his/her own parents in HH
gen ParentHasParent = (PotentialMidGenParent==1 & pnpt~=999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort suid addid srefmon
by suid addid srefmon: egen N_Parent_LiveWithParent = total(ParentHasParent)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
gen epn_gp=pnpt if ParentHasParent==1

** Count up number of distinct parents-of-parents
egen distinctgptag = tag(suid addid srefmon epn_gp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctGP = total(distinctgptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of a parent
sort suid addid srefmon epn_gp
by suid addid srefmon: egen temp_epn_gp1 = min(epn_gp)

replace epn_gp=. if epn_gp==temp_epn_gp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_gp2 = min(epn_gp)



** Generate an indicator, at the individual level, for being a parent of a parent (a grandparent)
** And then generate one for being a female grandparent
gen PotentialOldGenParent=(pnum==temp_epn_gp1 | pnum==temp_epn_gp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_gp1 = pnsp if pnum==temp_epn_gp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp1 = min(temp_epn_spouse_gp1)
gen SpouseOfGP1=(pnum==epn_spouse_gp1)
replace PotentialOldGenParent=1 if SpouseOfGP1==1

gen temp_epn_spouse_gp2 = pnsp if pnum==temp_epn_gp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_gp2 = min(temp_epn_spouse_gp2)
gen SpouseOfGP2=(pnum==epn_spouse_gp2)
replace PotentialOldGenParent=1 if SpouseOfGP2==1

gen PotentialOldGenMom = (PotentialOldGenParent==1 & sex==2)



** Make other demographic variables

gen Employed = (esr==1 | esr==2 | esr==3 | esr==4 | esr==5)

gen HSGrad = (higrade==12 & grdcmpl==1)
gen SomeCollege = (higrade==21 | higrade==22 | higrade==23 | (higrade==24 & grdcmpl==2))
gen CollegeGrad = ((higrade==24 & grdcmpl==1) | higrade==25 | higrade==26)

gen Female = (sex==2)

gen White = (race==1)

gen Married = (ms==1 | ms==2)

gen FTStudent=(enrold==1)

** Rename some variables in ways that will be consistent with 1996 and later panels
rename month rhcalmn
rename year rhcalyr
rename rrp errp
rename sustate tfipsst
rename earn tpearn
rename fearn tfearn
rename htotinc thtotinc
rename fnlwgt wpfinwgt

save TempData, replace

*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep suid addid entry swave srefmon pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1993_w7, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep suid addid entry swave srefmon pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec

rename socsec essself

duplicates drop

save OldGenParentData1993_w7, replace
clear


********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

use TempData

gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the potential empty nester has one of his/her own parents in HH
gen EmptyNesterHasParent = (PotentialEmptyNester==1 & pnpt~=999 )


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed parent of that mid-gen parent 
** NOTE: engp = empty nest grandparent
gen epn_engp=pnpt if EmptyNesterHasParent==1

** Count up number of distinct parents-of-parents
egen distinctengptag = tag(suid addid srefmon epn_engp)
sort suid addid srefmon
by suid addid srefmon: egen N_DistinctENGP = total(distinctengptag)



** At household-month level, record person numbers of up to 2 people 
** identified as parents of an empty-nester parent
sort suid addid srefmon epn_engp
by suid addid srefmon: egen temp_epn_engp1 = min(epn_engp)

replace epn_engp=. if epn_engp==temp_epn_engp1
sort suid addid srefmon epn_parent
by suid addid srefmon: egen temp_epn_engp2 = min(epn_engp)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent
gen PotentialEmptyNestGP=(pnum==temp_epn_engp1 | pnum==temp_epn_engp2 )

** Identify spouses of these grandparents
gen temp_epn_spouse_engp1 = pnsp if pnum==temp_epn_engp1
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp1 = min(temp_epn_spouse_engp1)
gen SpouseOfENGP1=(pnum==epn_spouse_engp1)
replace PotentialEmptyNestGP=1 if SpouseOfENGP1==1

gen temp_epn_spouse_engp2 = pnsp if pnum==temp_epn_engp2
sort suid addid srefmon 
by suid addid srefmon: egen epn_spouse_engp2 = min(temp_epn_spouse_engp2)
gen SpouseOfENGP2=(pnum==epn_spouse_engp2)
replace PotentialEmptyNestGP=1 if SpouseOfENGP2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & sex==2)

save Temp2Data, replace

******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep suid addid entry swave srefmon pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1993_w7, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep suid addid entry swave srefmon pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc socsec 

rename socsec essself

duplicates drop

save EmptyNesterGPData1993_w7, replace
clear




***************************************************************************
** 1996 PANEL
***************************************************************************

************
** Wave 3
************

quietly infile using sip96l3.dct, using(sipp96l3.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)


save TempData, replace





*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1996_w3, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData1996_w3, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 

** Can't use 1993 code exactly, because now there are separate identifiers 
** for mothers and fathers

use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)


** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=9999 )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid shhadid srefmon epn_engmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGMom = total(distinctengmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=9999 )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid shhadid srefmon epn_engdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(epppnum==temp_epn_engmom1 | epppnum==temp_epn_engmom2 | epppnum==temp_epn_engdad1 | epppnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if epppnum==temp_epn_engmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(epppnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if epppnum==temp_epn_engmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(epppnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if epppnum==temp_epn_engdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(epppnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if epppnum==temp_epn_engdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(epppnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace



******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData1996_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save EmptyNesterGPData1996_w3, replace
clear







************
** Wave 6
************

quietly infile using sip96l6.dct, using(sipp96l6.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)

** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)


** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace




*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1996_w6, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData1996_w6, replace
clear





************
** Wave 9
************

quietly infile using sip96l9.dct, using(sipp96l9.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)


** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace






*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep 

duplicates drop

save MidGenParentData1996_w9, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData1996_w9, replace
clear




************
** Wave 12
************

quietly infile using sip96l12.dct, using(sipp96l12.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)


** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)



** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace

*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData1996_w12, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save OldGenParentData1996_w12, replace
clear




**************************************************************************************************
** 2001
**************************************************************************************************

************
** Wave 3
************

quietly infile using sip01w3.dct, using(sipp01w3.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as dads of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a dad of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2001_w3, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData2001_w3, replace
clear




********************************************************************************
**  Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)


** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=9999 )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid shhadid srefmon epn_engmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGMom = total(distinctengmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=9999 )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid shhadid srefmon epn_engdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(epppnum==temp_epn_engmom1 | epppnum==temp_epn_engmom2 | epppnum==temp_epn_engdad1 | epppnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if epppnum==temp_epn_engmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(epppnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if epppnum==temp_epn_engmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(epppnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if epppnum==temp_epn_engdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(epppnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if epppnum==temp_epn_engdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(epppnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace



******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData2001_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save EmptyNesterGPData2001_w3, replace
clear




************
** Wave 6
************

quietly infile using sip01w6.dct, using(sipp01w6.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)


** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent  PotentialDep
duplicates drop

save MidGenParentData2001_w6, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData2001_w6, replace
clear




************
** Wave 9
************

quietly infile using sip01w9.dct, using(sipp01w9.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)



** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace

*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2001_w9, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save OldGenParentData2001_w9, replace
clear




**************************************************************************************************
** 2004
**************************************************************************************************


************
** Wave 3
************

quietly infile using sip04w3.dct, using(l04puw3.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace


*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2004_w3, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 
duplicates drop

save OldGenParentData2004_w3, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)


** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=9999 )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid shhadid srefmon epn_engmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGMom = total(distinctengmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=9999 )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid shhadid srefmon epn_engdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(epppnum==temp_epn_engmom1 | epppnum==temp_epn_engmom2 | epppnum==temp_epn_engdad1 | epppnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if epppnum==temp_epn_engmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(epppnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if epppnum==temp_epn_engmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(epppnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if epppnum==temp_epn_engdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(epppnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if epppnum==temp_epn_engdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(epppnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace



******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData2004_w3, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save EmptyNesterGPData2004_w3, replace
clear





************
** Wave 6
************

quietly infile using sip04w6.dct, using(l04puw6.dat)
destring epppnum, replace



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace




*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2004_w6, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData2004_w6, replace
clear





**************************************************************************************************
** 2008
**************************************************************************************************

************
** Wave 4
************

quietly infile using sippl08puw4.dct, using(l08puw4.dat)
destring epppnum, replace


** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)



** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if Under19_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace



*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2008_w4, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1


keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData2008_w4, replace
clear




********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.
** Eventual goal is to use wave-2 fertility history info on kids ever born
** to identify households in which that middle-gen person actually is a parent,
** but to kids living elsewhere. 


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)


** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=9999 )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid shhadid srefmon epn_engmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGMom = total(distinctengmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_engmom
by ssuid shhadid srefmon: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=9999 )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid shhadid srefmon epn_engdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid shhadid srefmon epn_engdad
by ssuid shhadid srefmon: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(epppnum==temp_epn_engmom1 | epppnum==temp_epn_engmom2 | epppnum==temp_epn_engdad1 | epppnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if epppnum==temp_epn_engmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(epppnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if epppnum==temp_epn_engmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(epppnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if epppnum==temp_epn_engdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(epppnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if epppnum==temp_epn_engdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(epppnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace



******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc 

duplicates drop

save EmptyNesterData2008_w4, replace
clear


*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data

keep if PotentialEmptyNestGP==1

keep ssuid shhadid swave srefmon epppnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married  rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save EmptyNesterGPData2008_w4, replace
clear



************
** Wave 7
************

quietly infile using sippl08puw7.dct, using(l08puw7.dat)
destring epppnum, replace


** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)



** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)


save TempData, replace




*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2008_w7, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1


keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself 

duplicates drop

save OldGenParentData2008_w7, replace
clear




************
** Wave 10
************

quietly infile using sippl08puw10.dct, using(l08puw10.dat)
destring epppnum, replace


** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=9999)
gen HaveDadinHH = (epndad~=9999)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))

gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)


** Count up number of such kids per household per month. 
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_PotentialDep = total(PotentialDep)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid shhadid srefmon: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen MinAge_PotentialDep = min(temp)
drop temp

** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid shhadid srefmon epn_mom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid shhadid srefmon epn_mom
by ssuid shhadid srefmon: egen temp_epn_mom4 = min(epn_mom)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(epppnum==temp_epn_mom1 | epppnum==temp_epn_mom2 | epppnum==temp_epn_mom3 | epppnum==temp_epn_mom4)

** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid shhadid srefmon epn_dad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads

** At household-month level, record person numbers of up to 4 people
** identified as moms of an under-19 kid living with parent
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid shhadid srefmon epn_dad
by ssuid shhadid srefmon: egen temp_epn_dad4 = min(epn_dad)

** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenDad=(epppnum==temp_epn_dad1 | epppnum==temp_epn_dad2 | epppnum==temp_epn_dad3 | epppnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)

** Drop intermediate variables that were used in the identification of spouses of parents-of-parents
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=9999 )

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct parents-of-parents
egen distinctgmomtag = tag(ssuid shhadid srefmon epn_gmom)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGMom = total(distinctgmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid shhadid srefmon epn_gmom
by ssuid shhadid srefmon: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=9999 )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own parents in HH
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid shhadid srefmon epn_gdad)
sort ssuid shhadid srefmon
by ssuid shhadid srefmon: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad

** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid shhadid srefmon epn_gdad
by ssuid shhadid srefmon: egen temp_epn_gdad2 = min(epn_gdad)



** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2 | epppnum==temp_epn_gdad1 | epppnum==temp_epn_gdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if epppnum==temp_epn_gmom1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(epppnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if epppnum==temp_epn_gmom2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(epppnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if epppnum==temp_epn_gdad1
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(epppnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if epppnum==temp_epn_gdad2
sort ssuid shhadid srefmon 
by ssuid shhadid srefmon: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(epppnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((epppnum==temp_epn_gmom1 | epppnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

gen HSGrad = (eeducate==39 | eeducate==41)
gen SomeCollege = (eeducate==40 | eeducate==42 | eeducate==43)
gen CollegeGrad = (eeducate==44 | eeducate==45 | eeducate==46 | eeducate==47)

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1)

save TempData, replace

*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid shhadid swave srefmon epppnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc FTStudent PotentialDep

duplicates drop

save MidGenParentData2008_w10, replace
clear

*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1


keep ssuid shhadid swave srefmon epppnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep rhcalmn rhcalyr wpfinwgt errp tfipsst tpearn tfearn thtotinc essself

duplicates drop

save OldGenParentData2008_w10, replace
clear




